package com.factory.test.activity;

import android.graphics.Point;
import android.hardware.Camera;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityCameraBinding;
import com.yecontech.sdk.base.camera.CameraCallBack;
import com.yecontech.sdk.base.camera.CameraException;
import com.yecontech.sdk.base.camera.CameraManager;

/**
 * 摄像头, 相机测试
 */
public class CameraActivity extends BaseTestActivity {
    private ActivityCameraBinding binding;
    private CameraManager rgbCameraManager;
    private CameraManager irCameraManager;
    private boolean irEnable = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_camera);

        rgbCameraManager = new CameraManager(context, rgbPreviewCallBack);
        rgbCameraManager.setManualCameraId(CameraManager.CAMERA_ID_RGB);      //相机ID
        rgbCameraManager.setPreviewSize(1920, 1080);           //设置相机的预览宽高(默认 1280 * 720)
        //rgbCameraManager.setCameraAngle(90);                               //设置相机预览角度(默认 90)
        binding.rgbView.setSurfaceTextureListener(rgbCameraManager);         //必须在onCreate中绑定SurfaceTexture
        //binding.rgbView.setScaleX(-1);   //左右镜像
        getLifecycle().addObserver(rgbCameraManager);                       //自动管理相机的生命周期


        if(irEnable) {
            irCameraManager = new CameraManager(context, irPreviewCallBack);
            irCameraManager.setManualCameraId(CameraManager.CAMERA_ID_IR);
            irCameraManager.setPreviewSize(1920, 1080);
            //irCameraManager.setCameraAngle(90);
            binding.irView.setSurfaceTextureListener(irCameraManager);
            //binding.irView.setScaleX(-1);   //左右镜像
            getLifecycle().addObserver(irCameraManager);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    private CameraCallBack rgbPreviewCallBack = new CameraCallBack() {
        @Override
        public void onInit(Camera camera) {

        }

        @Override
        public void onError(CameraException e) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    binding.rgbTip.setText(e.getMessage());
                }
            });
        }

        @Override
        public void onPreviewFrame(byte[] data, Camera camera, Point cameraResolution) {

        }
    };

    private CameraCallBack irPreviewCallBack = new CameraCallBack() {

        @Override
        public void onInit(Camera camera) {

        }


        @Override
        public void onError(CameraException e) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    binding.irTip.setText(e.getMessage());
                }
            });
        }

        @Override
        public void onPreviewFrame(byte[] data, Camera camera, Point cameraResolution) {

        }
    };

}
